<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="subjectForm"
        style="padding-right: 30px;"
        :rules="subjectInfosrules"
        ref="subjectForm"
      >

        <el-row>
          <el-col :span="11" style="margin-top: 27px;">
            <el-form-item class="form-item" label="模板名称 :" prop="name">
              <el-input v-model="subjectForm.name" placeholder="请输入模板名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="模板描述 :" prop="description">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                placeholder="请输入模板描述"
                v-model="subjectForm.description"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-col :span="20">
              <el-form-item class="form-item" label="类别 :" prop="category">
                <el-select
                  v-model="subjectForm.category"
                  placeholder="请选择"
                  style="width: 280px;"
                >
                  <el-option
                    v-for="item in natureDIC"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
       <el-row>
          <el-col :span="18">
            <el-col :span="10">
              <el-form-item class="form-item" label="责任部门:" prop="leadDeptId">
                {{subjectForm.supportDeptName}}
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;" @click="clickjoint">责任部门</el-button>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <div class="form-submit">
        <el-button type="primary" @click.stop="commitList()">添加</el-button>
        <el-button type="info" @click.stop="goBack()">关闭</el-button>
      </div>
    </div>
    <xieorg-tree :orgCodeVisiblesubmit="xieorgCodeVisiblesubmit" @orgCodeClick="xieorgCodeClick" @orgCodeHandleClosesubmit="xieorgCodeHandleClosesubmit" @orgCodeCheckChange="xieorgCodeCheckChange"></xieorg-tree>
  </basic-container>
</template>

<script>
  import {pimRules} from "@/const/infoRules";
  import {addModel} from "@/api/pim/pim";
  export default {
    name: "addModel",
    created(){
      this.subjectForm.pmuc = this.$route.params.pmuc
      this.subjectForm.planningTitle=this.$route.query.titleName
    },
    data(){
      return{
        headerData:{
          title:'新增模版'
        },
        subjectInfosrules:pimRules,// 表单验证
        subjectForm: {
          pmuc:'',
        },
        xieorgCodeVisiblesubmit:false,//责任部门树弹窗标志
        natureDIC:[
          {
            label:'指标',
            value:'1'
          },
          {
            label:'任务',
            value:'2'
          },
          {
            label:'项目',
            value:'3'
          },
        ]
      }
    },
    methods:{
      goBack() {
        this.$router.go(-1);
      },
      // 弹出责任部门弹框
      clickjoint() {
        this.xieorgCodeVisiblesubmit = true
      },
      xieorgCodeClick() {
      },
      xieorgCodeHandleClosesubmit() {
        this.xieorgCodeVisiblesubmit = false
      },
      xieorgCodeCheckChange(data) {
        if (data.length > 1) {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请仅选择一个责任部门",
            duration: 2000
          });
          return;
        } else {
          this.subjectForm.supportDeptName = data[0].name;
          this.subjectForm.leadDeptId = data[0].id;
        }
      },
      commitList(){
        if (this.subjectForm.leadDeptId == undefined) {
          this.$notify({
            title: '提示',
            message: '请选择责任部门',
            type: 'success',
            duration: 2000
          });
          return;
        }
        this.$confirm("确定添加模版?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$refs.subjectForm.validate(valid => {
            if (valid) {
              addModel(this.subjectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "添加成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(-1);
                }
              })
            }
          })
        })

      }
    }
  }
</script>

<style lang="scss" scoped>
  .card-border {
    border: 1px solid #d2d2d2;
    padding: 12px;
  }
  .form-submit {
    text-align: center;
  }
</style>
